﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThietBiSo.Models;
using ThietBiSo.LinQDB;

namespace ThietBiSo.Controllers
{
    public class xaydung_danhmucController : ApplicationController
    {
        //
        // GET: /loaitintuc/

        XayDung_DanhMuc ltt = new XayDung_DanhMuc();
        MXayDung_DanhMuc mltt = new MXayDung_DanhMuc();

        public ActionResult Index(int? id)
        {
            int pageString = id ?? 0;
            int page = pageString == 0 ? 1 : pageString;
            int total = mltt.GetTotal();
            PagingController("/xaydung_danhmuc/index/", total, page, "");
            ViewData["xaydung_danhmuc"] = mltt.GetList(start, offset);
            return View();
        }
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /TinTuc/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                ltt.MaDanhMuc = mltt.GetMax() + 1;
                ltt.TenDanhMuc = collection.Get("TenDanhMuc");

                ltt.HienThi = true;

                if (mltt.GetMaxTT() == 0 || mltt.GetTotal() == 0)
                {
                    ltt.ThuTu = 1;
                }
                else
                    ltt.ThuTu = mltt.GetMaxTT() + 1;

                mltt.Save(ltt);
                return RedirectToAction("index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Edit(int id)
        {
            ViewData["xaydung_danhmuc"] = mltt.GetByID(id);
            return View();
        }

        //
        // POST: /TinTuc/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                ltt.MaDanhMuc = id;
                ltt.TenDanhMuc = collection.Get("TenDanhMuc");

                ltt.ThuTu = int.Parse(collection.Get("ThuTu"));
                mltt.Update(ltt);
                return RedirectToAction("index");
            }
            catch
            {
                return View();
            }
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int ma)
        {

            string error = mltt.Delete(ma);
            if (!String.IsNullOrEmpty(error))
                return Json(error);
            return Json(String.Empty);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult HienThi(int ma)
        {

            string error = mltt.HienThi(ma);
            if (!String.IsNullOrEmpty(error))
                return Json(error);
            return Json(String.Empty);
        }

    }
}
